/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */
package acid.spring.jdbc.repositories;

import acid.spring.db.Experiment;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 *
 * @author morgan
 */
@Repository
public class JdbcExperimentRepository implements ExperimentRepository, InitializingBean
{

    @Value("${jdbc.sql.experiments.listAll}")
    private String listAllExperimentsQuery;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Experiment> listAllExperiments()
    {
        return jdbcTemplate.query(listAllExperimentsQuery, experimentRowMapper);

    }

    @Override
    public void afterPropertiesSet() throws Exception
    {
        Assert.notNull(this.jdbcTemplate, "the jdbcTemplate can't be null!");
        Assert.notNull(this.listAllExperimentsQuery, "the TissueByIdQuery can't be null");
    }
    private RowMapper<Experiment> experimentRowMapper = new RowMapper<Experiment>()
    {

        @Override
        public Experiment mapRow(ResultSet resultSet, int i) throws SQLException
        {
            int id = resultSet.getInt("experiment_id");
            String name = resultSet.getString("experiment_name");
            String description =resultSet.getString("description");
            String source = resultSet.getString("source");

            return new Experiment(id, name, description, source);
        }
    };
}
